// pages/owner/voteDetail/voteDetail.js
const app = getApp()
Page({

  data: {
    id: null,
    vote: {},
    selectOption: null,
    radioChange: [
    ]
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      selectOption: e.detail.value
    })
  },
  vote: function (e){
      // console.log('投票选项为序号', this.data.selectOption)
    var data = {
      option: this.data.selectOption,
      voteId: this.data.id,
    }

    app.sendPostRequest('/community/owner/voteInfo/', data, (res) => {
      console.log('业委会投票结果：', res)
      if(res.code == 200){
        wx.showToast({
          title: '投票成功!',
          icon: 'success',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: '您已经投过票了！',
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    app.sendGetRequest('/community/owner/vote/'+this.data.id, (res) => {
      console.log('业委会投票', res)
      this.setData({
        vote: res.data
      })
      var optionList = this.data.vote.options.split(",")
      var optionDictList = []
      for(var i=0; i<optionList.length; i++){
        optionDictList.push({
          value: i,
          name: optionList[i]
        })
      }
      this.setData({
        radioChange: optionDictList
      })
      console.log('业委会投票：', this.data.vote)
    }, (err) => {
      console.log(err.errMsg)
    })
  },
})